﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TexasHoldem.RankedHands;

namespace TexasHoldem
{
    public class TexasHoldemEvaluator
    {
        IHandRanker _ranker = null;

        public TexasHoldemEvaluator(IHandRanker handRanker = null)
        {
            this._ranker = handRanker ?? new HandRanker();
        }

        public IEnumerable<string> EvaluateHands(IEnumerable<string> inlineHands)
        {
            var rankedHands = inlineHands.Select(h => _ranker.RankHand(h));
            var rankedHandsByStrength = rankedHands.OrderByDescending(h => h);
            return rankedHandsByStrength.Select(h => ComposeEvaluatedHandDescription(h, rankedHandsByStrength.First()));
        }

        private string ComposeEvaluatedHandDescription(RankedHand hand, RankedHand bestHand)
        {
            var cardsPart = new Hand(hand.OrderedCards).ToString();
            var rankingPart = hand.Cards.Count() == bestHand.Cards.Count() ? " " + hand.RankName : "";
            var winnerPart = hand.CompareTo(bestHand) == 0 ? " (winner)" : "";
            return cardsPart + rankingPart + winnerPart;
        }
    }
}
